package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.OrderConfirmAdapter;
import com.dlwx.firefighting.bean.OrderConfirmBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderConfirmActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.balance_rb)
    RadioButton balanceRb;
    @BindView(R.id.integral_rb)
    RadioButton integralRb;
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.order_recycler)
    LRecyclerView orderRecycler;
    @BindView(R.id.money_tv)
    TextView moneyTv;
    @BindView(R.id.confirm_tv)
    TextView confirmTv;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.phone_tv)
    TextView phoneTv;
    @BindView(R.id.adress_al)
    AutoRelativeLayout adressAl;
    private String receIds;
    private String userId;
    private String shippingId;
    private OrderConfirmAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private boolean isAddressExist=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_confirm);
        ButterKnife.bind(this);
        receIds = getIntent().getStringExtra("id");
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getDatas();
    }

    private void initView() {
        tvMiddleTitle.setText("订单确认");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        mAdapter = new OrderConfirmAdapter(this);
        mAdapter.setDataList(new ArrayList<OrderConfirmBean.GoodsListBean>());
        orderRecycler.setLayoutManager(new LinearLayoutManager(this));
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        orderRecycler.setAdapter(lRecyclerViewAdapter);
        orderRecycler.setLoadMoreEnabled(false);
        orderRecycler.setPullRefreshEnabled(false);
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.divider_height_1_5)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.app_bg_color)
                .build();
        orderRecycler.addItemDecoration(divider);
        orderRecycler.refresh();
    }

    private void getDatas() {
        //        1、删除购物车中某一个商品2、清空购物车
        OkGo.<String>post(Constant.CHECK_ORDER)
                .tag(this)
                .params("userid", userId)
                .params("recid_data", receIds)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<OrderConfirmBean> beans = dealResult.deal(OrderConfirmActivity.this, response, new TypeToken<BaseBean<OrderConfirmBean>>() {
//                        }.getType());
//                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(OrderConfirmActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<OrderConfirmBean> beans) {
         mAdapter.clear();
        if (beans.isEmpty()) {
            return;
        }
        List<OrderConfirmBean.GoodsListBean> lists = beans.get(0).getGoods_list();
        OrderConfirmBean.ConsigneeListBean consigneeBean = beans.get(0).getConsignee_list();
        addressTv.setText("收货地址：" +consigneeBean.getAddress());
        if(TextUtils.isEmpty(consigneeBean.getAddress())){
            addressTv.setText("请先添加收货地址！");
            isAddressExist=false;
        }else{
            isAddressExist=true;
        }
        nameTv.setText("收货人："+consigneeBean.getConsignee());
        phoneTv.setText(consigneeBean.getMobile());
        moneyTv.setText(getString(R.string.rmb) + beans.get(0).getTotal());
        shippingId = beans.get(0).getShipping_id() + "";
        if (lists.isEmpty()) {
            return;
        }
        mAdapter.addAll(lists);
        orderRecycler.refresh();
    }

    @OnClick({R.id.title_left_one_btn, R.id.balance_rb, R.id.integral_rb, R.id.address_tv, R.id.confirm_tv,R.id.adress_al})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                OrderConfirmActivity.this.finish();
                break;
            case R.id.balance_rb:
                balanceRb.setChecked(true);
                integralRb.setChecked(false);
                break;
            case R.id.integral_rb:
                integralRb.setChecked(true);
                balanceRb.setChecked(false);
                break;
            case R.id.address_tv:
                break;
            case R.id.confirm_tv:
                if(!isAddressExist){
                    ToastUtils.toastForShort(OrderConfirmActivity.this,"请先添加收货地址！");
                    return;
                }
                confirm();
                break;
            case R.id.adress_al:
                startActivity(new Intent(OrderConfirmActivity.this,AddressListActivity.class));
                break;
        }
    }

    private void confirm() {
        DialogUtils.showProgressDlg(this,"");
//        payId,
        OkGo.<String>post(Constant.CREATE_ORDER)
                .tag(this)
                .params("userid", userId)
                .params("shipping", 3)
                .params("payment", 4)
                .params("recid_data", receIds)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        BasicBean basicBean=dealResult.dealBasic(OrderConfirmActivity.this, response);
//                        if(basicBean==null){
//                            return;
//                        }
//                        if(basicBean.getStatus()==Constant.ERROR){
//                            ToastUtils.toastForShort(OrderConfirmActivity.this,basicBean.getMsg());
//                            return;
//                        }
//                        OrderFinishBean bean = dealResult.dealOne(OrderConfirmActivity.this, response, new TypeToken<BaseOneBean<OrderFinishBean>>() {
//                        }.getType());
//                        if (bean != null) {
//                            Intent i = new Intent(OrderConfirmActivity.this, OrderDetailsActivity.class);
//                            i.putExtra("id",bean.getOrder_id()+"");
//                            OrderConfirmActivity.this.finish();
//                            startActivity(i);
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(OrderConfirmActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
